# coding:utf-8

from os import path
import numpy as np
from PIL import Image

import chnSegment
import plotWordcloud



def preprocess_mask_img(input_image_path, new_width):
    # 打开图像 -- 等比放大，保证高分辨率
    with Image.open(input_image_path) as img:
        # 获取原始图像的尺寸
        original_width, original_height = img.size
        
        # 计算新的高度以保持宽高比
        new_height = int((new_width / original_width) * original_height)
        
        # 调整图像大小（等比放大）
        resized_img = img.resize((new_width, new_height))
        
        return np.array(resized_img)


if __name__=='__main__':

    # 读取文件
    d = path.dirname(__file__)
    ############ 按需修改最后的路径；输入文件放在'doc/'
    text = open(path.join(d,'doc', 'result-en.txt')).read()

    ############ 停用词
    stop_words = {'通过', '基于', '用于', '使用', '对象', '进行', '利用', '方法', 'via', '', '', '', '', '', '', '', '', }

    # mask
    # cvpr25svg_path = path.join("images", "cvpr-navbar-logo.svg")
    # mask_ndarray = svg_to_mask(cvpr25svg_path, 4000)
    # mask_ndarray = None
    mask_img_path = path.join("images", "cvpr25logo-内部填充.png")
    mask_img = preprocess_mask_img(mask_img_path, 4000)

    # 若是中文文本，则先进行分词操作
    text=chnSegment.word_segment(text)
    
    # 生成词云
    plotWordcloud.generate_wordcloud(text, "CVPR25-英文标题词云_logo透明完整", stop_words, mask_img)
